package edu.osu.intelligentagents.assignment1;

import java.util.Random;

public class Assignment1ScenarioGenerator {
	private static float correctFloat(float f) {
		return ((int) (10 * f)) / 10.f;
	}

	public static void main(String[] args) {
		int goldMines = 1;
		int rows = 3;
		int cols = 3;
		float fraction = 0.9f;
		int[][] maze = new int[rows][cols];
		Random random = new Random(System.currentTimeMillis());
		for (int i = 0; i < goldMines; i++) {
			int row = random.nextInt(rows);
			int col = random.nextInt(cols);
			if (maze[row][col] == 0) {
				maze[row][col] = 1;
			} else {
				i--;
			}
		}

		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				System.out.print(maze[row][col] + ",");
			}
		}
		System.out.println();
		int n = rows * cols;
		int m = 4;
		// The basic idea behind this generator is to give high probability to
		// most probable next state and
		float[][] transition1 = new float[n][n];
		float[][] transition2 = new float[n][n];
		float[][] transition3 = new float[n][n];
		float[][] transition4 = new float[n][n];
		for (int s1 = 0; s1 < n; s1++) {
			for (int s2 = 0; s2 < n; s2++) {
				transition1[s1][s2] = 0;
				transition2[s1][s2] = 0;
				transition3[s1][s2] = 0;
				transition4[s1][s2] = 0;
			}
		}

		// generating transition function for first action (right)
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				if (col != cols - 1) {
					transition1[row * (cols) + col][row * (cols) + col + 1] = fraction;
				} else {
					transition1[row * (cols) + col][row * (cols) + col] = fraction;
				}
				float leftP = correctFloat(1 - fraction);
				while (leftP > 0) {
					if (row != 0) {
						transition1[row * (cols) + col][(row - 1) * (cols)
								+ col] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);
					}
					if (row != rows - 1 && leftP > 0) {
						transition1[row * (cols) + col][(row + 1) * (cols)
								+ col] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);

					}
				}
			}
		}

		// generating transition function for second action (left)
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				if (col != 0) {
					transition2[row * (cols) + col][row * (cols) + col - 1] = fraction;
				} else {
					transition2[row * (cols) + col][row * (cols) + col] = fraction;
				}
				float leftP = correctFloat(1 - fraction);
				while (leftP > 0) {
					if (row != 0) {
						transition2[row * (cols) + col][(row - 1) * (cols)
								+ col] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);
					}
					if (row != rows - 1 && leftP > 0) {
						transition2[row * (cols) + col][(row + 1) * (cols)
								+ col] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);

					}

				}
			}
		}

		// generating transition function for third action (up)
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				if (row != 0) {
					transition3[row * (cols) + col][(row - 1) * (cols) + col] = fraction;
				} else {
					transition3[row * (cols) + col][row * (cols) + col] = fraction;
				}
				float leftP = correctFloat(1 - fraction);
				while (leftP > 0) {
					if (col != 0) {
						transition3[row * (cols) + col][row * (cols) + col - 1] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);
					}
					if (col != cols - 1 && leftP > 0) {
						transition3[row * (cols) + col][row * (cols) + col + 1] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);

					}

				}
			}
		}

		// generating transition function for fourth action (down)
		for (int row = 0; row < rows; row++) {
			for (int col = 0; col < cols; col++) {
				if (row != rows - 1) {
					transition4[row * (cols) + col][(row + 1) * (cols) + col] = fraction;
				} else {
					transition4[row * (cols) + col][row * (cols) + col] = fraction;
				}
				float leftP = correctFloat(1 - fraction);
				while (leftP > 0) {
					if (col != 0) {
						transition4[row * (cols) + col][row * (cols) + col - 1] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);
					}
					if (col != cols - 1 && leftP > 0) {
						transition4[row * (cols) + col][row * (cols) + col + 1] += 0.1f;
						leftP = correctFloat(leftP - 0.1f);

					}

				}
			}
		}
		System.out.println("Transition for action right");
		for (int s1 = 0; s1 < n; s1++) {
			for (int s2 = 0; s2 < n; s2++) {
				System.out.print(transition1[s1][s2] + ",");
			}
			System.out.println();
		}
		System.out.println("Transition for action left");
		for (int s1 = 0; s1 < n; s1++) {
			for (int s2 = 0; s2 < n; s2++) {
				System.out.print(transition2[s1][s2] + ",");
			}
			System.out.println();
		}
		System.out.println("Transition for action up");
		for (int s1 = 0; s1 < n; s1++) {
			for (int s2 = 0; s2 < n; s2++) {
				System.out.print(transition3[s1][s2] + ",");
			}
			System.out.println();
		}
		System.out.println("Transition for action down");
		for (int s1 = 0; s1 < n; s1++) {
			for (int s2 = 0; s2 < n; s2++) {
				System.out.print(transition4[s1][s2] + ",");
			}
			System.out.println();
		}

	}
}
